package ui;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


import service.*;
import model.*;
import util.*;


public class Login extends JFrame{
    private LoginService loginService=LoginService.getInstance();
    private Toolkit toolkit = Toolkit.getDefaultToolkit(); // 获得系统默认工具类
    private Dimension sc = toolkit.getScreenSize(); // 获得屏幕尺寸

    private JLabel title = new JLabel("欢迎使用");
    private JLabel name = new JLabel("账 号");
    private JLabel pass = new JLabel("密 码");
    private JTextField textName = new JTextField(20); // 普通文本框
    private JPasswordField textPass = new JPasswordField(20); // 密码框


    private JRadioButton choice1 = new JRadioButton("用户");
    private JRadioButton choice2 = new JRadioButton("管理员");


    private JButton button1 = new JButton("注册");
    private JButton button2 = new JButton("登录");


    private Font font1 = new Font("华文中宋", Font.PLAIN, 40);
    private Font font2 = new Font("华文新魏", Font.PLAIN, 25); // 字体，样式，大小

    private ButtonGroup buttonGroup = new ButtonGroup();
  public Login() {
	  setLayout(new GridLayout(5,1));
	  
	  setResizable(false);
      setLocation((sc.width - 500) / 2, (sc.height - 600) / 2);
      
      
	  title.setFont(font1);
	  name.setFont(font2);
	  pass.setFont(font2);
	  textPass.setEchoChar('*');// 可以将密码显示为* ；默认为·
	  choice1.setFont(font2);
	  choice1.setSelected(true);// 默认普通用户登录
	  choice2.setFont(font2);
	  button1.setFont(font2);
	  button2.setFont(font2);
	  
	  buttonGroup.add(choice1);
	  buttonGroup.add(choice2);
	  JPanel[] panel= {new JPanel(),new JPanel(),new JPanel(),new JPanel(),new JPanel(),new JPanel()};//panel默认flowLayout
	  panel[0].add(title);
	  panel[0].setBackground(Color.cyan);
	  panel[1].add(name);
	  panel[1].add(textName);
	  panel[1].setBackground(Color.cyan);
	  panel[2].add(pass);
	  panel[2].add(textPass);
	  panel[2].setBackground(Color.cyan);
	  panel[3].add(choice1);
	  panel[3].add(choice2);
	  panel[3].setBackground(Color.cyan);
	  panel[4].add(button1);
	  panel[4].add(button2);
	  panel[4].setBackground(Color.cyan);
	  panel[4].setLayout(new FlowLayout(FlowLayout.CENTER,100,0));//增大两按钮间距离
	  for(int i=0;i<5;i++) {
		  add(panel[i]);
	  }
	  button1.addActionListener(new ActionListener() {
		 public void actionPerformed(ActionEvent e) {
			 setVisible(false);
         	textName.setText(null);
         	textPass.setText(null);  
			 
			 Register.registerShow();
			 
			 
		 }
	  });
	  
	  
	  button2.addActionListener(new ActionListener() {
		  public void actionPerformed(ActionEvent e) {
			  String id = textName.getText();
	            String pswd = new String(textPass.getPassword());

	            if (id.equals("") || pswd.equals("")) {
	                ShowMessage.show("账号、密码不能为空！");
	            } else {
                          int readId=0;
	                    try {
	                        // String型id转int型id，不可转（非数字字符）时会抛异常
	                      readId = Integer.parseInt(id);
	                    } catch (Exception e1) {
	                        e1.printStackTrace();
	                        // 故需捕获之
	                        ShowMessage.show("请输入有效的账号！");
	                    }
	                    if (choice1.isSelected()) {  
	                    	User u=loginService.userLogin(readId, pswd);
	                        if( u==null) {
	                        	ShowMessage.show("账号或密码错误！");
	                        }else {
	                        	setVisible(false);
	                        	textName.setText(null);
	                        	textPass.setText(null);
	 	                       UserUI.userShow(u.getUid());
	                        }
	                    }else if(choice2.isSelected()) {
	                    	Admin a=loginService.adminLogin(readId, pswd);
	                        if( a==null) {
	                        	ShowMessage.show("账号或密码错误！");
	                        }else {
	                        	setVisible(false);
	                        	textName.setText(null);
	                        	textPass.setText(null);                        	
	                        	AdminUI.adminShow();
	                        }

	                    }
	            }
		  }
		  });


	  
  }
  
  public static void loginShow() {
	  JFrame login=new Login();
	  login.setVisible(true);
	  login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	  login.setTitle("境外人员酒店隔离管理系统");
	  login.setSize(500,600);  

  }
  


}
